package com.jboost.modules.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jboost.common.api.vo.Result;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.constant.FeishuTzEnum;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.app.dto.DpBanDan;
import com.jboost.modules.app.dto.HsOrderCountDto;
import com.jboost.modules.app.dto.MoodytigerNoiceDto;
import com.jboost.modules.app.dto.resp.HsOrderCountResp;
import com.jboost.modules.app.service.ChatGptService;
import com.jboost.modules.app.service.strategry.pdm.PdmBanDanQuery;
import com.jboost.modules.app.util.StrategyFactory;
import com.jboost.modules.system.entity.PdmPl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Api(tags = "客户管理")
@RestController
@RequestMapping("/pdm")
@Slf4j
@AllArgsConstructor
public class PdmController implements Strategy {


    ChatGptService chatGptService;
    PdmBanDanQuery pdmBanDanQuery;

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "j2tq9lljcelsqTPZnbr36USxr6yZWsHu");
        return jdyHeaders;
    }

   static String appid ;
    static String fomId;
    public String getAppId(){
        return appid;
    }


    public String getFormId(){
        return fomId;
    }


    @PostMapping(value = "/banDanPush")
    @ApiOperation(value = "版单数据推送", notes = "版单数据推送")
    public synchronized void banDanPush(@RequestBody JSONObject jsonObject, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        CompletableFuture.runAsync(()->{
            try {
                pdmBanDanQuery.invokeStrategy(jsonObject);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

    }

    @PostMapping(value = "/abPush")
    @ApiOperation(value = "数据推送", notes = "数据推送")
    public synchronized void push(@RequestBody JSONObject jsonObject, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        LinkedHashMap data = jsonObject.getObject("data", LinkedHashMap.class);
        appid = (String) data.get("appId");
        fomId = (String) data.get("entryId");
        jdyUpdate((String) data.get("_id"),DataBeanReq.builder().b(getParaMap(data.get("a"))).build());
    }

    @Data
    @Builder
    public static class DataBeanReq{
        private Map<String,Object> b;
    }

    @GetMapping(value = "/transfer")
    @ApiOperation(value = "数据转换", notes = "数据转换")
    public Result<String> push(@RequestParam(name = "msg") String msg) throws Exception {

        String replace = msg.replace(",", "-");
        return Result.OK(replace);
    }

    @GetMapping(value = "/bdsj")
    @ApiOperation(value = "cj5", notes = "cj5")
    public List<Map> cj5(@RequestParam(name = "wldl_list",required = false,defaultValue = "") String wldl_list,
                         @RequestParam(name = "wlxl_list",required = false,defaultValue = "") String wlxl_list,
                              @RequestParam(name = "wlbm_list",required = false,defaultValue = "") String wlbm_list,
                         @RequestParam(name = "wlmc_list",required = false,defaultValue = "") String wlmc_list,
                              @RequestParam(name = "gg_list",required = false,defaultValue = "") String gg_list,
                              @RequestParam(name = "dw_list",required = false,defaultValue = "") String dw_list,
                         @RequestParam(name = "hsdj_list",required = false,defaultValue = "") String hsdj_list,
                         @RequestParam(name = "gyswlbm_list",required = false,defaultValue = "") String gyswlbm_list,
                         @RequestParam(name = "ywlbm_list",required = false,defaultValue = "") String ywlbm_list
    ) throws Exception {
        List<Map> list = Lists.newArrayList();
        String[] wldl = wldl_list.split(",");
        String[] wlxl = wlxl_list.split(",");
        String[] wlbm = wlbm_list.split(",");
        String[] wlmc = wlmc_list.split(",");
        String[] gg = gg_list.split(",");
        String[] dw = dw_list.split(",");
        String[] hsdj = hsdj_list.split(",");
        String[] gyswlbm = gyswlbm_list.split(",");
        String[] ywlbm = ywlbm_list.split(",");

        for (int i = 0; i < wldl.length; i++) {
            Map map = new HashMap();
            try {
                map.put("wldl", wldl[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("ywlbm", ywlbm[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("wlxl", wlxl[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("wlbm", wlbm[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("wlmc", wlmc[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("gg", gg[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("dw", dw[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("hsdj", hsdj[i]);
            } catch (Exception exception) {

            }
            try {
                map.put("gyswlbm", gyswlbm[i]);
            } catch (Exception exception) {

            }
            list.add(map);
        }

        return list;
    }


    @GetMapping(value = "/kh")
    @ApiOperation(value = "cj7", notes = "cj7")
    public List<PdmPl> cj7(@RequestParam(name = "kh") String kh) throws Exception {
        return (List<PdmPl>) ((BaseResult)StrategyFactory.invokeStrategy(kh,"PdmPlQuery")).getT();
    }


    private List<HsOrderCountDto> trasnfer(String field4, HsOrderCountDto hsOrderCountDto) {
        List<HsOrderCountDto> list = Lists.newArrayList();
        String[] split = field4.split("\\-");
        for (String s : split) {
            String[] split1 = s.split("\\*");
            if (!Objects.equals(split1[0],"0")) {
                HsOrderCountDto hsOrderCountDto1 = new HsOrderCountDto();
                BeanUtil.copyProperties(hsOrderCountDto, hsOrderCountDto1);
                hsOrderCountDto1.setField4(split1[0]);
                hsOrderCountDto1.setField6(Integer.parseInt(split1[1]) * Integer.parseInt(hsOrderCountDto1.getField5()) + "");
                list.add(hsOrderCountDto1);
            }else {
                HsOrderCountDto hsOrderCountDto1 = new HsOrderCountDto();
                BeanUtil.copyProperties(hsOrderCountDto, hsOrderCountDto1);
                hsOrderCountDto1.setField6("0");
                list.add(hsOrderCountDto1);
            }
        }
        return list;
    }

    private HsOrderCountDto getByCLass(String s) throws IllegalAccessException {
        HsOrderCountDto hsOrderCountDto = new HsOrderCountDto();
        Field[] declaredFields = hsOrderCountDto.getClass().getDeclaredFields();
        String[] split = s.split("\\|");
        for (int i = 0; i < declaredFields.length; i++) {
            declaredFields[i].set(hsOrderCountDto, split[i]);
        }
        return hsOrderCountDto;
    }


}
